use serde::{Deserialize, Serialize};
use tool::tool_result::Error;

#[derive(Serialize, Deserialize)]
pub struct Config {
    pub mysql_list: Vec<MysqlConfig>,
    pub mysql_list_example: MysqlConfig,
}

impl Config {
    pub fn new() -> Config {
        Config {
            mysql_list: MysqlConfig::test(),
            mysql_list_example: MysqlConfig::new_example(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MysqlConfig {
    pub name: String,
    pub is_docker: bool,
    pub docker_container_name: String,
    pub auto_backup_time: String,
    pub host: String,
    pub port: u16,
    pub user: String,
    pub password: String,
    pub database: String,
    pub not_backup_table: Vec<String>,
}

impl MysqlConfig {
    pub fn new_example() -> Self {
        MysqlConfig{
            name:"此为实例模板 这一行是备份项名  只有auto_backup_time，not_backup_table可以不填写内容".to_string(),
            is_docker:false,
            docker_container_name:"这里是Docker容器名 如果是非docker可忽略".to_string(),
            auto_backup_time:"23:30".to_string(),
            host:"127.0.0.1".to_string(),
            port:3306,
            user:"root".to_string(),
            password:"123456".to_string(),
            database:"test".to_string(),
            not_backup_table:["user".to_string()].to_vec()
        }
    }
    pub fn test() -> Vec<MysqlConfig> {
        vec![
            MysqlConfig {
                name: "伊哈赛事数据库".to_string(),
                is_docker: false,
                docker_container_name: "".to_string(),
                auto_backup_time: "23:30".to_string(),
                host: "127.0.0.1".to_string(),
                port: 3306,
                user: "root".to_string(),
                password: "feelwee".to_string(),
                database: "ea_jw".to_string(),
                not_backup_table: [].to_vec(),
            },
            MysqlConfig {
                name: "test".to_string(),
                is_docker: false,
                docker_container_name: "".to_string(),
                auto_backup_time: "23:30".to_string(),
                host: "127.0.0.1".to_string(),
                port: 3306,
                user: "root".to_string(),
                password: "feelwee".to_string(),
                database: "test".to_string(),
                not_backup_table: [].to_vec(),
            },
        ]
    }

    pub fn to_home(&self) -> MysqlConfigHome {
        MysqlConfigHome {
            name: self.name.clone(),
            is_docker: self.is_docker,
            docker_container_name: self.docker_container_name.clone(),
            auto_backup_time: self.auto_backup_time.clone(),
            host: self.host.clone(),
            port: self.port,
            user: self.user.clone(),
            password: self.password.clone(),
            database: self.database.clone(),
            not_backup_table: self.not_backup_table.clone(),
            today_atuo_backup: false,
        }
    }

    pub fn get_auto_backup_time(&self) -> Result<(u64, u64), Error> {
        if self.auto_backup_time == "" {
            return Err(Error::new("未设置auto_backup_time"));
        }
        let time_list: Vec<&str> = self.auto_backup_time.split(":").collect();
            if time_list.len() != 2 {
                return Err(Error::new("auto_backup_time格式错误，应为HH:mm"));
            }
            let hour = tool::tool_convert::StringConvert::to_int_u64(time_list[0]);
            let mm: Result<u64, Error> =
                tool::tool_convert::StringConvert::to_int_u64(time_list[1]);
            if hour.is_err() || mm.is_err() {
                return Err(Error::new(
                    "auto_backup_time格式错误，应为HH:mm 且必须是>=0的整数",
                ));
            }
            let hour=hour.unwrap();
            let mm=mm.unwrap();
            if hour > 23 || mm > 59 {
                return Err(Error::new(
                    "auto_backup_time格式错误，小时不能大于23，分钟不能大于59",
                ));
            }
            // if hour == 0 && mm < 30{
            //     return Err(Error::new(
            //         "[00:00,00:30)这段时间不允许设置自动备份",
            //     ));
            // }
            Ok((hour,mm))
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MysqlConfigHome {
    pub name: String,
    pub is_docker: bool,
    pub docker_container_name: String,
    pub auto_backup_time: String,
    pub host: String,
    pub port: u16,
    pub user: String,
    pub password: String,
    pub database: String,
    pub not_backup_table: Vec<String>,
    pub today_atuo_backup: bool,
}

impl MysqlConfigHome {
    pub fn get_auto_backup_time(&self) -> Result<(u64, u64), Error> {
        if self.auto_backup_time != "" {
            return Err(Error::new("未设置auto_backup_time"));
        }
        let time_list: Vec<&str> = self.auto_backup_time.split(":").collect();
            if time_list.len() != 2 {
                return Err(Error::new("auto_backup_time格式错误，应为HH:mm"));
            }
            let hour = tool::tool_convert::StringConvert::to_int_u64(time_list[0]);
            let mm: Result<u64, Error> =
                tool::tool_convert::StringConvert::to_int_u64(time_list[1]);
            if hour.is_err() || mm.is_err() {
                return Err(Error::new(
                    "auto_backup_time格式错误，应为HH:mm 且必须是>=0的整数",
                ));
            }
            let hour=hour.unwrap();
            let mm=mm.unwrap();
            if hour > 23 || mm > 59 {
                return Err(Error::new(
                    "auto_backup_time格式错误，小时不能大于23，分钟不能大于59",
                ));
            }
            // if hour == 0 && mm < 30{
            //     return Err(Error::new(
            //         "[00:00,00:30)这段时间不允许设置自动备份",
            //     ));
            // }
            Ok((hour,mm))
    }
}